using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using sfcms.api;
using System.Reflection;
using System.Text;

public partial class sfcms_sfcms_console : System.Web.UI.UserControl
{
    private static void Recurse(Control c1, PlaceholderActionDelegate actionHandler)
    {
        foreach (Control testControl in c1.Controls)
        {
            iplaceholderuicontrol testPlaceholder = testControl as iplaceholderuicontrol;
            if (testPlaceholder != null)
            {
                actionHandler(testPlaceholder);
            }
            else
            {
                Recurse(testControl, actionHandler);
            }
        }
    }

    private delegate void PlaceholderActionDelegate(iplaceholderuicontrol placeholder);

    protected void Page_Load(object sender, EventArgs e)
    {
        if (page.current == null || sfcms.api.user.current == null || Request.QueryString["sfcmspreview"] == "1")
        {
            this.Visible = false;
        }

        if (!this.Visible)
        {
            Recurse(Page, delegate(iplaceholderuicontrol placeholdercontrol) { placeholdercontrol.DisplayPlaceHolder(); });
            return;
        }

        if (IsPostBack)
        {
            if (string.IsNullOrEmpty(pagename.Value))
            {
                Response.Redirect(page.current.friendlyurl);
                return;
            }
            
            long parentID = 0;
            if (long.TryParse(Request.QueryString["parent"], out parentID))
            {
                folder testfolder = folder.byid(parentID);
                if (testfolder != null)
                {
                    if (testfolder.type != 1) throw new InvalidOperationException("The parent folder is not a web page folder");
                }
            }

            page.current.name = pagename.Value;
            page.current.displayname = pagedisplayname.Value;
            page.current.save();

            if (page.current.parent == null)
            {
                page.current.setparent(parentID);
            }

            Recurse(Page, delegate(iplaceholderuicontrol placeholdercontrol) { placeholdercontrol.SavePlaceHolder(); });

            Response.Redirect(page.current.friendlyurl);
        }
        else
        {
            // this.Page.ClientScript.RegisterClientScriptBlock(GetType(), "ONBEFOREUNLOAD", "function obu() { return \"Your changes will be lost. Do you want to continue? \"}; window.onbeforeunload = obu;", true);
            folder parent;
            if(page.current.id > 0)
            {
                parent = page.current.parent;
                previewlink.HRef = "javascript:document.forms[0].target = 'pagepreview';document.forms[0].action='" + page.current.friendlyurl + "?sfcmspreview=1';document.forms[0].submit();";
            }
            else
            {
                parent = folder.byid(Convert.ToInt64(Request.QueryString["parent"]));
            }
            
            StringBuilder pathBuilder = new StringBuilder(parent == null ? "&nbsp;" : parent.displayname);
            while(parent != null)
            {
                if (parent.parentid != 0)
                {
                    parent = folder.byid(parent.parentid);
                    pathBuilder.Insert(0, parent.displayname + " / ");
                }
                else
                {
                    parent = null;
                }
            }
            pagelocation.Text = pathBuilder.ToString();
            pagename.Value = page.current.name;
            pagedisplayname.Value = page.current.displayname;
            Recurse(Page, delegate(iplaceholderuicontrol placeholdercontrol) { placeholdercontrol.DisplayPlaceHolder(); });
        }
    }
}
